package
{
	import starling.events.Event;

	public class UIManager extends GameObject
	{
		private var _backStack:Array;
		private var _forwardStack:Array;
		private var _currentScreen:UIScreen;
		
		public function UIManager()
		{
			super();
			_backStack = new Array();
			_forwardStack = new Array();
			
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(UINavigateEvent.UI_GOTO, gotoUIScreen);
			this.addEventListener(UINavigateEvent.UI_BACK, onBackClicked);
		}
								
		private function onAddedToStage(event:Event):void
		{
			this.removeEventListener(starling.events.Event.ADDED_TO_STAGE, null);
			if(_backStack.length == 0)
				_currentScreen = new PlayScreen();
			else
				_currentScreen = _backStack.pop() as UIScreen;
			addChild(_currentScreen);	
			
			_currentScreen.onEnter();
		}		
		
		/** Transitioning Code **/
		//this will be called every frame. 
		//we can use it for our transitioning effect. 
		//It will not do anything if there is no transitioning going on.
		
		private var bIsTransitioning:Boolean = false; //used to turn transitioning mode on/off
		private var _toScreen:UIScreen; //used to hold reference to the screen we are transitioning to.
		private var _backNav:Boolean = false; //used to indicate if we are navigating back
		
		/**
		 * used for triggering ui screen transition 
		 * from currentScreen to _toScreen 		 
		 **/
		private function gotoUIScreen(event:UINavigateEvent):void
		{
			var screenID:String = event.screenID;
			
			if(screenID == "Game")
			{
				_currentScreen.onExit();
				//remove listeners from currentScreen
				_currentScreen.removeEventListeners(); // do this in onTransitionCompleted 
				//remove currentScreen from display list
				_currentScreen.removeFromParent();// do this in onTransitionCompleted						
				//add currentScreen to backStack.
				_backStack.push(_currentScreen);// do this in onTransitionCompleted
				
				(this.parent as Game).onLevelStart();
			}			
			else
			{
				//turn on transitioning
				bIsTransitioning = true;
				
				if(screenID == "SelectScreen")
					_toScreen = new SeasonSelectScreen(); //assign new screen to toScreen instead of currentScreen
					
				else if(screenID == "LevelSelect")
					_toScreen = new LevelSelect(event.seasonID); //assign new screen to toScreen instead of currentScreen
				
				_currentScreen.touchable = false;
				_toScreen.touchable = false;
				_toScreen.alpha = 0;
				addChild(_toScreen);
				
				
			}
		}
		
		/**
		 * used for triggering ui screen transition 
		 * from currentScreen to _toScreen in backward direction 		 
		 **/
		private function onBackClicked(event:UINavigateEvent):void
		{
			bIsTransitioning = true;
			_backNav = true;
			_toScreen = _backStack.pop() as UIScreen;
			_currentScreen.touchable = false;
			_toScreen.touchable = false;
			_toScreen.alpha = 0;			
			addChild(_toScreen);						
		}

		public override function onEnterFrame(dt:Number):void
		{
			//if in transitioning mode then do transitioning between
			// currentScreen and toScreen, otherwise just return.
			if(bIsTransitioning)
			{
				//add actual transitioning code here which will change 
				//alpha of currentScreen from 1 to 0 (subtract dt from current value of currentScreen.alpha every frame)
				_currentScreen.alpha -= dt;
				//& alpha of toScreen from 0 to 1. (add dt to current value of toScreen.alpha every frame)
				_toScreen.alpha += dt;
				
				//when transitioning is finished (i.e. toScreen.alpha == 1)
				if(_toScreen.alpha == 1)
				{
					// then call function 'onTransitionCompleted'
					onTransitionCompleted();					
				}									
			}
		}		

		/**
		 * Used for doing post-transition stuff
		 **/
		private function onTransitionCompleted():void
		{
			//turn off transitioning
			bIsTransitioning = false;
			//remove listeners from currentScreen
			_currentScreen.removeEventListeners(); // do this in onTransitionCompleted 
			//remove currentScreen from display list
			_currentScreen.removeFromParent();// do this in onTransitionCompleted
			_currentScreen.onExit();
			if(_backNav)
			{
				_backNav = false;
				_currentScreen.dispose();
				_currentScreen = null; 
			}
			else
			{				
				_currentScreen.alpha = 1;
				_currentScreen.touchable = true;
				//add currentScreen to backStack.
				_backStack.push(_currentScreen);// do this in onTransitionCompleted				
			}

			_toScreen.touchable = true;			
			
			_toScreen.onEnter();
			//assign toScreen to currentScreen.
			_currentScreen = _toScreen;			
			//assign null to toScreen;
			_toScreen = null;
		}
	}
}